import axios from 'axios'
import {message} from 'antd'
import router from '../router/index';
import { createHashHistory,createBrowserHistory } from 'history';

const history = createHashHistory();
let http = axios.create({
    timeout:10000,
})


http.interceptors.request.use(req=>{
    //后台数据返回查看
    let token = sessionStorage.getItem('token')
    token && (req.headers['Authorization'] = `Bearer ${token}`)
    return req
},error=>Promise.reject(error))

http.interceptors.response.use(resp=>{
    //后台数据返回查看
    resp.data.token && sessionStorage.setItem('token',resp.data.token)
    return resp
},error=>{
    let _resp = error.response
    switch (_resp.status) {
        case 401 :
            //核对数据
            sessionStorage.removeItem('token')
            sessionStorage.removeItem('username')
            // sessionStorage.removeItem('jobId')
            message.error('身份已过期，请重新登录！')
            // return router.push('/login')
            return history.push('/login')
    }
    return Promise.reject(error.response.data)
})

export default http